package com.sky.task;

import com.sky.constant.MessageConstant;
import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单定时任务
 *
 * @author 熊柯
 * @Description:
 * @Date: 2024/9/3
 * @date 2024/09/03
 */
@Component
@Slf4j
public class OrderTask {

    @Resource
    OrdersMapper ordersMapper;

    /*
     * 每分钟查询未付款超时订单
     * */
    @Scheduled(cron = "0 * * * * ?")
    public void processTeimeOutOrder() {
        log.info("每分钟查询超时订单task：{}", LocalDateTime.now());
        // 根据订单状态查询 当前时间-15分钟 > 查询出的时间
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-15);
//        LocalDateTime localDateTime = LocalDateTime.now().plusSeconds(-30); //测试
        List<Orders> ordersList = ordersMapper.queryBYOrderTimeLTANDStatus(Orders.PENDING_PAYMENT, localDateTime);
        for (Orders orders : ordersList) {
            orders.setStatus(Orders.CANCELLED);
            orders.setCancelReason("订单超时，自动取消");
            orders.setCancelTime(LocalDateTime.now());
            ordersMapper.update(orders);
        }


    }


    @Scheduled(cron = "0 0 1 * * ? ")
    public void deliveryTimeOutOrder() {
        log.info("每日凌晨查询超时订单task：{}", LocalDateTime.now());
        List<Orders> ordersList = ordersMapper.queryBYOrderTimeLTANDStatus(Orders.DELIVERY_IN_PROGRESS, LocalDateTime.now().plusHours(-1));
        //将派送中的设置完完成
        for (Orders orders : ordersList) {
            orders.setStatus(Orders.CANCELLED);
            ordersMapper.update(orders);
        }
    }


}
